import axios from 'axios'
import {ElMessage, ElMessageBox} from 'element-plus'
import {useUserStore} from '@/store/user'
import {getCookies} from '@/utils/storage'
import defaultSettings from '@/settings'

// 业务请求
const request = axios.create({
    baseURL: defaultSettings.isMockData ? '' : import.meta.env.VITE_APP_BASE_API, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    timeout: 5000 // request timeout
})

// request interceptor
request.interceptors.request.use(
    (config) => {
        // do something before request is sent
        const userStore = useUserStore()

        if (userStore.token) {
            // let each request carry token
            // ['X-Token'] is a custom headers key
            // please modify it according to the actual situation
            config.headers['X-Token'] = getCookies('Fanqie-Token')
        }
        return config
    },
    (error) => {
        // do something with request error
        console.log(error) // for debug
        return Promise.reject(error)
    }
)

// response interceptor
request.interceptors.response.use(
    /**
     * If you want to get http information such as headers or status
     * Please return  response => response
     */

    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    (response) => {
        const res = response.data
        // if the custom code is not 20000, it is judged as an error.
        if (res.code !== 200) {
            ElMessage({
                message: res.msg || 'Error',
                type: 'error',
                duration: 5 * 1000
            })

            // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
            if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
                // to re-login
                ElMessageBox.confirm(
                    'You have been logged out, you can cancel to stay on this page, or log in again',
                    'Confirm logout',
                    {
                        confirmButtonText: 'Re-Login',
                        cancelButtonText: 'Cancel',
                        type: 'warning'
                    }
                ).then(() => {
                    const userStore = useUserStore()
                    userStore.resetToken.then(() => {
                        location.reload()
                    })
                })
            }
            return Promise.reject(new Error(res.msg || 'Error'))
        } else {
            return res
        }
    },
    (error) => {
        console.log('err' + error) // for debug
        ElMessage({
            message: error.msg,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
)

/**
 * 用于请求 gitee 的数据
 */
const requestA = axios.create({
    baseURL: import.meta.env.VITE_APP_GITEE_BASE_API, // url = base url + request url
    timeout: 60 * 1000
})

requestA.interceptors.request.use(
    (config) => {
        return config
    },
    (error) => {
        console.log(error)
        return Promise.reject(error)
    }
)

requestA.interceptors.response.use(
    (response) => {
        const {data, status, statusText} = response
        if (status === 200) {
            return data
        } else {
            ElMessage({
                message: statusText || 'Error',
                type: 'error',
                duration: 5 * 1000
            })
            return false
        }
    },
    (error) => {
        console.log('err' + error)
        ElMessage({
            message: error.msg,
            type: 'error',
            duration: 5 * 1000
        })
        return Promise.reject(error)
    }
)

export {request, requestA}
